package com.xiaode.interceptor;

import cn.hutool.core.util.ArrayUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.xiaode.common.anotation.UserType;
import com.xiaode.exce.EnumError;
import java.lang.reflect.Method;

/**
 * 在登录验证之后
 * 用户类型拦截老师学生家长等
 */
public class UserTypeInterceptor implements Interceptor {

    @Override
    public void intercept(Invocation inv) {
    	
        Controller c = inv.getController();
    	Integer userType = c.getAttr("userType");
    	
    	Method method = inv.getMethod();
    	UserType conUserType = method.getAnnotation(UserType.class);// 获取接口所需要的权限
    	int[] conTypes = conUserType.conType();
    	
    	//判断用户类型是否符合
    	if(!ArrayUtil.contains(conTypes, userType)) {
        	c.renderJson(Ret.fail().set("error", EnumError.NOT_HAVE_AUTHORITY.getObj()));
            return;
        }
    	
        inv.invoke();
    }

}
